import { Vec2 } from 'cc';
import { Vec3 } from 'cc';
import { MeshRenderer } from 'cc';
import { _decorator, ColliderComponent, Component, ICollisionEvent, Node } from 'cc';
import { RigidBody } from 'cc';
import { Tween } from 'cc';
import { director } from 'cc';
import { PhysicsSystem } from 'cc';
import { PhysicsMaterial } from 'cc';
import { instantiate } from 'cc';
import { LoadMg } from '../tools/LoadMg';
import { App } from '../App';
import { Prefab } from 'cc';
import { SpriteFrame } from 'cc';
import { Asset } from 'cc';
import { ItemType } from '../type';
import { CurveRange } from 'cc';
import { tween } from 'cc';
import { Intersection2D } from 'cc';
import { SphereCollider } from 'cc';
import { find } from 'cc';
import { Ball } from './billiards/Ball';
import GameUtils2 from './billiards/mode2/GameUtils2';
import { loadCommonPreFab, showErrorTip, showTipBox } from '../tools/PopuUp';
import { MatchWnd } from '../hall/multiplay/MatchWnd';
import { MultiRule } from '../hall/multiplay/MultiRule';
import { RoomCreateLayer } from '../hall/multiplay/RoomCreateLayer';
import { ScrollView } from 'cc';
import { tweenNumber } from '../../../extensions/build_game_auto/@types/packages/scene/@types/cce/3d/manager/camera/tween';
import { RoomItem } from '../hall/multiplay/RoomItem';
import { Button } from 'cc';
import UtilTool from '../tools/UtilTool';
import { Quat } from 'cc';
import { ModelComponent } from 'cc';
import { Mesh } from 'cc';
import { desk } from './desk';
import { v3 } from 'cc';
import { EventHandler } from 'cc';
import { Reward } from '../hall/Reward';
const { ccclass, property } = _decorator;

let BALL_POS_INTERVAL = 0
let OFFEST_CNETER_X = 20

enum FFEnum {
    A = "A",
    B = "B",
    C = "C",
}

@ccclass('test1')
export class test1 extends Component {  

    @property([Node])
    nodeList : Node[] = [];
    
    // @property(Prefab)
    // ballPrefab : Prefab = null

    @property(EventHandler)
    testFunc : EventHandler = null

    
    ball : Node = null;

    start() {

        // this.test1()

        // showTipBox(App.language.getLanguageLab(2026))

        // this.showLayer()

        
        // this.test1()

        // this.test2()

        this.test3()
    }

    async test1() {
        // showErrorTip(255)

        // PhysicsSystem.instance.autoSimulation = false

        // this.initBallList()

        // let ballNode = this.nodeList[0]

        // let body : RigidBody = ballNode.getComponent(RigidBody)
        // body.clearForces()
        // body.clearState()

        // let btn = this.nodeList[0].getComponent(Button)
        // btn.interactable = false
        // btn.enabled = false

        // this.outputAllRigibody()

        // let node = await loadCommonPreFab('prefabs/hall/invite', null, false)
        // let node = await loadCommonPreFab('prefabs/hall/multiplay/BeMasterWnd', null, false)

        // let node = await loadCommonPreFab('prefabs/hall/inviteRecord', null, false)

    }

    

    test2() {
        let rooItem : RoomItem = this.nodeList[0].getComponent(RoomItem)

        let data = {
            canWatch : true,
            roomId : "test00" + 1,
            gameType : 0,
            status : 1,
            maxPlayer :3,
            currentPlayerNum: 2,
            playAmount : 100,
            havePassword : false,
        }

        rooItem.initData(data, 1)
    }

    onScroll(event, eventType) {

        let typeStr = Object.keys(ScrollView.EventType)[eventType]
        UtilTool.printLog("test onScroll", eventType, typeStr)
    }

    initBallList() {
        
        let ballNode = this.nodeList[0]
        ballNode.getComponent(RigidBody).useCCD = true
        let gameNode = find("gameNode")

        let index = 0
        for(let i=1; i<6; i++) {

            for(let j=0; j<i; j++) {
                index++
                let ball = instantiate(ballNode)

                let width = 1

                let totalWidth = (i - 1)* BALL_POS_INTERVAL + i * width
                
                let x = (i-1)*(BALL_POS_INTERVAL+width*Math.sqrt(3)/2) + OFFEST_CNETER_X;

                let z = -totalWidth / 2 + j * (BALL_POS_INTERVAL+width) + width / 2;
                let pos = new Vec3(x, 1, z);
                
                ball.setPosition(pos);
                ball.active = true  
                ball.parent = gameNode
            }
        }

        // 设置力度
        tween(this.node).delay(2).call(() => {
            let rigidBody = ballNode.getComponent(RigidBody)
            rigidBody.applyLocalImpulse(new Vec3(250, 0, 0), new Vec3(0, 0, 0))
        }).start()

    }

    async showLayer() {
        let node = await loadCommonPreFab("prefabs/hall/multiplay/RoomCreateLayer", null, false)
        // let node = await loadCommonPreFab("prefabs/hall/multiplay/RoomJoinLayer", null, false)
        node.getComponent(RoomCreateLayer).initModifyUi({
            canWatch : true,
            maxPlayer : 3,
            gameType : 0,
            playAmount : 1000,
            password : 123,
        }, 0, false)
        // node.getComponent(MatchWnd).initData(0, 1)
    }

    update(delta) {
        
        // let ball = this.nodeList[0]

        // UtilTool.printLog("ball pos ", ball.getWorldPosition().toString())

        // PhysicsSystem.instance.step(0.008, 0.008, 10)
        
        
    }


    // 打印所有刚体信息
    outputAllRigibody() {

        let dataList = []

        let outOne = (node : Node) => {
            let rigidbody : ModelComponent = node.getComponent(ModelComponent)
            if(rigidbody) {

                let rotate = node.eulerAngles

                let data = {
                    name : node.name,
                    pos : node.worldPosition.toString(),
                    rotation : rotate.toString(),
                    size : UtilTool.getModelSize(node).toString(),
                }

                UtilTool.printLog("data", data)

                dataList.push(data)
            }
        }

        let parent = this.nodeList[0]

        let roleParentList = parent.children.filter((item) => {
            return item.name.indexOf("wall_role") != -1
        })
        
        let edgeList1 = find('edge_list1', parent).children
        let edgeList2 = find('edge_list2', parent).children
        let edgeList3 = find('edge_list3', parent).children
        let edgeRoleList = find('edge_role_list', parent).children
        let roleList = find('role_list', parent).children
        let wallList = find('wall_list', parent).children

        let plane = find('plane', parent)
        let allList = wallList.concat(roleParentList).concat(roleList).concat(edgeList1).concat(edgeList2).concat(edgeList3).concat(edgeRoleList)
        allList.push(plane)

        allList.forEach((item) => {
            outOne(item)
        })

        let text = ""
        // {{121.50, 4.63, 0.41}, {0.00, 0.00, 0.00}, {10.00, 20.00, 128.00}},
        // allList.forEach((item) => {
            // text += UtilTool.formatStr('{%s, %s, %s}', item.pos.toString())
        // })
        

        UtilTool.printLog("wallList", wallList.length, edgeList1.length, edgeList2.length, edgeList3.length, edgeRoleList.length, roleList.length)

    }

    test3() {

        // http://localhost:7550?invite=20037

        UtilTool.printLog("tets1")

        tween(this.node).delay(1).call(async () => {

            let node = await loadCommonPreFab('prefabs/hall/reward',null, false)
            node.getComponent(Reward).initData(10)

            UtilTool.printLog("tets2")

        }).start()

        

    }

}